import exchange from '@service.exchange'

export function getUa() {
  return new Promise((resolve, reject) => {
    try {
      exchange.get({
        key: 'MS_UA',
        scope: 'global',
        success(data) {
          try {
            if (typeof data == 'object') {
              if (data.value && data.value.split) {
                resolve(data.value.split(' hap')[0])
              }
            } else {
              if (data && data.split) {
                resolve(data.split(' hap')[0])
              }
            }
          } catch (e) {
            reject()
          }
        },
        fail: reject
      })
    } catch (e) {
      reject()
      console.error(e)
    }
  })
}

export function setUa(ua) {
  return new Promise((resolve, reject) => {
    exchange.set({
      ...ua,
      scope: 'global',
      success: resolve,
      fail: reject
    })
  })
}
